package com.zhida.smartcontrol.pagectrl;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.jay.tools.base.BaseDialogFragment;
import com.jay.tools.base.BaseFragment;
import com.jay.tools.recycler.OnItemClick;
import com.jay.tools.recycler.decoration.RecyclerViewItemDecoration;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.EventBusUtils;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.dialog.DelFailedDialogFragment;
import com.zhida.smartcontrol.launch.bottom.BottomItemFragment;
import com.zhida.smartcontrol.pagectrl.delprojectdialog.DelProjectDialogFragment;
import com.zhida.smartcontrol.pagectrl.editproject.EditProjectFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.ProjectDetailFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/10/9 009 9:02
 * description: 智控首页
 */
public class PageCtrlFragment extends BottomItemFragment<PageCtrlContract.View, PageCtrlPresenter> implements PageCtrlContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.tv_zhengshi)
    TextView tv_zhengshi;
    @BindView(R.id.tv_ceshi)
    TextView tv_ceshi;

    private PageCtrlAdapter mAdapter;
    private List<ProjectListBean> zhengshi = new ArrayList<>();
    private List<ProjectListBean> ceshi = new ArrayList<>();
    private boolean isCe=false;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_page_ctrl;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        mRView.setLayoutManager(new LinearLayoutManager(getProxyActivity()));
        mRView.addItemDecoration(new RecyclerViewItemDecoration.Builder(getProxyActivity())
                .thickness(DimenUtils.dp2px(getProxyActivity(), 1))
                .color(ResUtil.getColor(getProxyActivity(), R.color.c_F6F7F8))
                .create());

        mAdapter = new PageCtrlAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        EventBusUtils.register(this);
        mPresenter.requestProjectList();
        tv_zhengshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCe=false;
                mAdapter.setNewData(zhengshi);
                tv_zhengshi.setTextColor(Color.parseColor("#3862FF"));
                tv_ceshi.setTextColor(Color.parseColor("#333333"));
            }
        });
        tv_ceshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isCe=true;
                mAdapter.setNewData(ceshi);
                tv_ceshi.setTextColor(Color.parseColor("#3862FF"));
                tv_zhengshi.setTextColor(Color.parseColor("#333333"));
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(ProjectListBean event) {
        mPresenter.requestProjectList();
    }

    @Override
    public void showList(List<ProjectListBean> projectList) {
        zhengshi.clear();
        ceshi.clear();
        for (int i = 0; i < projectList.size(); i++) {
            if (projectList.get(i).getCategoryTxt().equals("正式项目")) {
                zhengshi.add(projectList.get(i));
            } else {
                ceshi.add(projectList.get(i));
            }
        }
        if (isCe) {
            mAdapter.setNewData(ceshi);
        }else {
            mAdapter.setNewData(zhengshi);
        }
    }

    @OnClick({R.id.ll_add})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.ll_add:
                open(EditProjectFragment.newInstance("", Const.TYPE_ADD));
                break;
        }
    }

    @Override
    protected void initLazyEvents() {
        mAdapter.setClickListener(new OnItemClick<ProjectListBean>() {
            @Override
            public void onClick(ProjectListBean info, int tag) {
                switch (tag) {
                    case 0:
                        open(ProjectDetailFragment.newInstance(info.getProjectName(), info.getId(), ProjectDetailFragment.FROM_CTRL));
                        break;
                    case 1:
                        open(EditProjectFragment.newInstance(info.getId(), Const.TYPE_EDIT));
                        break;
                    case 2:
                        DelProjectDialogFragment fragment = DelProjectDialogFragment.newInstance(info.getId());
                        fragment.setIDelResultListener(new DelProjectDialogFragment.IDelResultListener() {
                            @Override
                            public void delResult(boolean isOk, String msg) {
                                if (isOk) {
                                    toast(msg);
                                    mPresenter.requestProjectList();
                                } else {
                                    DelFailedDialogFragment.newInstance(msg).showDialog(getChildFragmentManager());
                                }
                            }
                        });
                        fragment.showDialog(getChildFragmentManager());
                        break;
                }
            }
        });

        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.requestProjectList();
                mRefresh.setRefreshing(false);
            }
        });
    }

    private void open(BaseFragment fragment) {
        getParentDelegate().start(fragment);
    }


}
